package com.ruoyi.training.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.training.domain.Curriculum;
import com.ruoyi.training.domain.ExamRecord;
import com.ruoyi.training.domain.TrainPlan;
import com.ruoyi.training.domain.TrainPlanCurriculum;
import com.ruoyi.training.mapper.CurriculumMapper;
import com.ruoyi.training.mapper.ExamRecordMapper;
import com.ruoyi.training.mapper.TrainPlanCurriculumMapper;
import com.ruoyi.training.mapper.TrainPlanMapper;
import com.ruoyi.training.service.ITrainPlanService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 培训计划管理Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class TrainPlanServiceImpl implements ITrainPlanService {
    @Resource
    private TrainPlanMapper trainPlanMapper;
    @Resource
    private TrainPlanCurriculumMapper trainPlanCurriculumMapper;
    @Resource
    private CurriculumMapper curriculumMapper;
    @Resource
    private ExamRecordMapper examRecordMapper;

    /**
     * 查询培训计划管理
     *
     * @param id 培训计划管理主键
     * @return 培训计划管理
     */
    @Override
    public TrainPlan selectTrainPlanById(Long id) {
        TrainPlan trainPlan = trainPlanMapper.selectTrainPlanById(id);

        List<TrainPlanCurriculum> planCurricula = trainPlanCurriculumMapper.selectTrainPlanCurriculumList(new TrainPlanCurriculum() {{
            setPlanId(id);
        }});

        planCurricula.forEach(item -> {
            Curriculum curriculum = curriculumMapper.selectCurriculumById(item.getCurriculumId());
            if (curriculum != null) {
                item.setCurriculumName(curriculum.getName());
            }
        });

        trainPlan.getParams().put("curriculums", planCurricula);

        return trainPlan;
    }

    /**
     * 查询培训计划管理列表
     *
     * @param trainPlan 培训计划管理
     * @return 培训计划管理
     */
    @Override
    public List<TrainPlan> selectTrainPlanList(TrainPlan trainPlan) {

        if (trainPlan.getParams().containsKey("createTimeStart") && trainPlan.getParams().containsKey("createTimeEnd")) {
            if (trainPlan.getParams().get("createTimeStart") != null && !"".equals(trainPlan.getParams().get("createTimeStart"))
                    && trainPlan.getParams().get("createTimeEnd") != null && !"".equals(trainPlan.getParams().get("createTimeEnd"))) {
                trainPlan.getParams().put("createTimeStart", trainPlan.getParams().get("createTimeStart") + " 00:00:00");
                trainPlan.getParams().put("createTimeEnd", trainPlan.getParams().get("createTimeEnd") + " 23:59:59");
            }

        }

        if (trainPlan.getParams().containsKey("startTimeStart") && trainPlan.getParams().containsKey("startTimeEnd")) {
            if (trainPlan.getParams().get("startTimeStart") != null && !"".equals(trainPlan.getParams().get("startTimeStart"))
                    && trainPlan.getParams().get("startTimeEnd") != null && !"".equals(trainPlan.getParams().get("startTimeEnd"))) {
                trainPlan.getParams().put("startTimeStart", trainPlan.getParams().get("startTimeStart") + " 00:00:00");
                trainPlan.getParams().put("startTimeEnd", trainPlan.getParams().get("startTimeEnd") + " 23:59:59");
            }

        }

        if (trainPlan.getParams().containsKey("examTimeStart") && trainPlan.getParams().containsKey("examTimeEnd")) {
            if (trainPlan.getParams().get("examTimeStart") != null && !"".equals(trainPlan.getParams().get("examTimeStart"))
                    && trainPlan.getParams().get("examTimeEnd") != null && !"".equals(trainPlan.getParams().get("examTimeEnd"))) {
                trainPlan.getParams().put("examTimeStart", trainPlan.getParams().get("examTimeStart") + " 00:00:00");
                trainPlan.getParams().put("examTimeEnd", trainPlan.getParams().get("examTimeEnd") + " 23:59:59");
            }

        }

        List<TrainPlan> trainPlans = trainPlanMapper.selectTrainPlanList(trainPlan);
        trainPlans.forEach(item -> {
            List<ExamRecord> examRecords = examRecordMapper.selectExamRecordList(new ExamRecord() {{
                setDeptId(trainPlan.getDeptId());
                setPlanId(item.getId());
                setUserId(trainPlan.getSelectByWho());
            }});
            if (!examRecords.isEmpty()) {
                //已报名
                item.setSignFlag(1);
            } else {
                item.setSignFlag(0);
            }
        });
        return trainPlans;
    }

    /**
     * 新增培训计划管理
     *
     * @param trainPlan 培训计划管理
     * @return 结果
     */
    @Override
    public int insertTrainPlan(TrainPlan trainPlan) {
        trainPlan.setCreateTime(DateUtils.getNowDate());
        int i = trainPlanMapper.insertTrainPlan(trainPlan);
        if (i > 0) {
            List<TrainPlanCurriculum> curriculums = JSON.parseArray(JSON.toJSONString(trainPlan.getParams().get("curriculums")), TrainPlanCurriculum.class);
            trainPlanCurriculumMapper.insertTrainPlanCurriculumBatch(new HashMap<String, Object>() {{
                curriculums.forEach(item -> {
                    item.setPlanId(trainPlan.getId());
                });
                put("curriculums", curriculums);
            }});
        }
        return i;
    }

    /**
     * 修改培训计划管理
     *
     * @param trainPlan 培训计划管理
     * @return 结果
     */
    @Override
    public int updateTrainPlan(TrainPlan trainPlan) {
        trainPlan.setUpdateTime(DateUtils.getNowDate());
        int i = trainPlanMapper.updateTrainPlan(trainPlan);
        if (i > 0) {
            List<TrainPlanCurriculum> curriculums = JSON.parseArray(JSON.toJSONString(trainPlan.getParams().get("curriculums")), TrainPlanCurriculum.class);
            //删除关联的课程数据
            trainPlanCurriculumMapper.deleteTrainPlanCurriculumByPlanIds(new Long[]{trainPlan.getId()});

            List<TrainPlanCurriculum> addList = new ArrayList<>(10);
            curriculums.forEach(item -> {
                if (item.getId() == null) {
                    addList.add(item);
                }
            });
            //新增关联
            trainPlanCurriculumMapper.insertTrainPlanCurriculumBatch(new HashMap<String, Object>() {{
                addList.forEach(item -> {
                    item.setPlanId(trainPlan.getId());
                });
                put("curriculums", addList);
            }});
            /*List<TrainPlanCurriculum> addList = new ArrayList<>(10);
            List<TrainPlanCurriculum> updateList = new ArrayList<>(10);
            curriculums.forEach(item -> {
                if (item.getId() != null) {
                    updateList.add(item);
                } else {
                    addList.add(item);
                }
            });
            curriculums.clear();

            //新增
            trainPlanCurriculumMapper.insertTrainPlanCurriculumBatch(new HashMap<String, Object>() {{
                addList.forEach(item -> {
                    item.setPlanId(trainPlan.getId());
                });
                put("curriculums", addList);
            }});

            //修改
            updateList.forEach(item -> {
                trainPlanCurriculumMapper.updateTrainPlanCurriculum(item);
            });*/

        }
        return i;
    }

    /**
     * 批量删除培训计划管理
     *
     * @param ids 需要删除的培训计划管理主键
     * @return 结果
     */
    @Override
    public int deleteTrainPlanByIds(Long[] ids) {
        int i = trainPlanMapper.deleteTrainPlanByIds(ids);
        if (i > 0) {
            //批量删除关联
            trainPlanCurriculumMapper.deleteTrainPlanCurriculumByPlanIds(ids);
        }
        return i;
    }

    /**
     * 删除培训计划管理信息
     *
     * @param id 培训计划管理主键
     * @return 结果
     */
    @Override
    public int deleteTrainPlanById(Long id) {
        return trainPlanMapper.deleteTrainPlanById(id);
    }
}
